package com.mall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrService;
import com.mall.product.service.CategoryService;
import com.mall.product.service.impl.AttrServiceImpl;
import com.mall.product.vo.AttrGroupWithAttrVo;
import com.mall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import com.common.utils.PageUtils;
import com.common.utils.R;


/**
 * ���Է���
 *
 * @author yjv
 * @email 111111@163.com
 * @date 2024-08-04 00:42:44
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrServiceImpl attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogfId){
        List<AttrGroupWithAttrVo> list = attrGroupService.getAttrgroupWithAttrsByCatelogId(catelogfId);
        return R.ok().put("data",list);
    }
    @PostMapping("/attr/relation")
    public R saveBatch(@RequestBody List<AttrRelationVo> vos){
        attrAttrgroupRelationService.saveBatchVo(vos);
        return R.ok();
    }
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNNNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String,Object> params){
        PageUtils page = attrService.getNoAttrRelationn(attrgroupId,params);
        return R.ok().put("page",page);
    }
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrRelationVo[] vos){
        attrService.deleteRelationn(vos);
        return R.ok();
    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.attrRelationAttr(attrgroupId);
        return R.ok().put("data",list);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{cateLogId}")
    public R list(@PathVariable("cateLogId") Long cateLogId,@RequestParam Map<String,Object> params) {
//        PageUtils page = attrGroupService.queryPage(cateLogId);
        PageUtils page = attrGroupService.queryPage(params,cateLogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long aLong = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCateLogPath(aLong);
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.saveNotRepeat(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
